Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/share/public_paths/[...id].tsx
5808 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { join } from "path";
7
import NextHead from "next/head";
8
9
import basePath from "lib/base-path";
10
import getPublicPathInfo from "lib/share/get-public-path-info";
11
import shareURL from "lib/share/share-url";
12
import withCustomize from "lib/with-customize";
13
import { getPublicPathNames } from "lib/names/public-path";
14
import PublicPath, { PublicPathProps } from "components/path/path";
15
16
import ogShareLogo from "public/logo/og-share-logo.png";
17
18
export default (props: PublicPathProps) => (
19
<>
20
<PublicPath {...props} />
21
<NextHead>
22
<meta property="og:type" content="article" />
23
<meta property="og:title" content={props.path} />
24
25
{props.description && (
26
<meta property="og:description" content={props.description} />
27
)}
28
{props.ogUrl && <meta property="og:url" content={props.ogUrl} />}
29
{props.ogImage && <meta property="og:image" content={props.ogImage} />}
30
{props.created && (
31
<meta property="article:published_time" content={props.created} />
32
)}
33
{props.last_edited && (
34
<meta property="article:modified_time" content={props.last_edited} />
35
)}
36
37
{/* Prevent search engine indexing of unlisted content or proxied content from external URLs */}
38
{(props.unlisted ||
39
(props.url &&
40
(props.url.startsWith("github/") ||
41
props.url.startsWith("gist/")))) && (
42
<>
43
<meta name="robots" content="noindex, nofollow" />
44
<meta name="googlebot" content="noindex, nofollow" />
45
</>
46
)}
47
</NextHead>
48
</>
49
);
50
51
export async function getServerSideProps(context) {
52
const id = context.params.id[0];
53
const relativePath = context.params.id.slice(1).join("/");
54
try {
55
const names = await getPublicPathNames(id);
56
if (names != null) {
57
// redirect
58
let location = join(
59
basePath,
60
names.owner,
61
names.project,
62
names.public_path,
63
);
64
if (context.params.id.length > 1) {
65
location = join(
66
location,
67
"files",
68
context.params.id.slice(1).join("/"),
69
);
70
}
71
return { props: { redirect: location } };
72
}
73
const props: PublicPathProps = await getPublicPathInfo({
74
id,
75
relativePath,
76
req: context.req,
77
});
78
79
const customize = await withCustomize({ context, props });
80
81
if (customize?.props?.customize != null) {
82
// Add full URL for social media sharing
83
//
84
customize.props.ogUrl = `${customize.props.customize.siteURL}${shareURL(
85
id,
86
relativePath,
87
)}`;
88
89
// Add image path for social media sharing
90
//
91
customize.props.ogImage =
92
customize.props.customize.logoSquareURL ||
93
`${customize.props.customize.siteURL}${ogShareLogo.src}`;
94
}
95
96
return customize;
97
} catch (_err) {
98
console.log(_err);
99
return { notFound: true };
100
}
101
}
102
103